/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.ui.renderer.SettingRenderer;
import com.anhndt.audiobook.utils.DBUtil;
import com.anhndt.audiobook.utils.DialogUtil;
import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListCellRenderer;
import java.util.Vector;

/**
 *
 * @author Sun
 */
public class SettingForm extends BaseForm {

    Vector settings;

    public SettingForm(BaseForm previous) {
        super(Resource.TITLE_SETTING, previous, null);
        createItemEntitySetting();
        init();
    }

    protected void init() {
        Display.getInstance().setThirdSoftButton(false);
        super.init();
        createListItem(settings, new SettingRenderer());
    }

    void createItemEntitySetting() {
        settings = new Vector();
        //THEMES
        String nameTheme = DBUtil.getValue(DBUtil.DB_RECORD_THEME_NAME);
        if (StringUtil.isNullOrEmpty(nameTheme)) {
            nameTheme = Resource.TXT_DEFAULT;
        }
//        ItemEntity themes = new ItemEntity("" + DBUtil.DB_RECORD_THEME_ID, nameTheme, Resource.SETTING_THEMES_CAPTION);
//        //Display Counter
        ItemEntity displayCounter = new ItemEntity("" + DBUtil.DB_RECORD_DISPLAY_COUNTER, "" + Util.displayCounter, Resource.SETTING_DISPLAY_COUNTER);
        Vector vdisplayCounter = displayCounter.getItemChildrends("" + ItemEntity.TYPE_SETTING);
        String[] sdisplayCounter = new String[]{"10", "15", "20", "25"};
        StringUtil.convertStringToVector(vdisplayCounter, sdisplayCounter);
        //Display Thumb
        ItemEntity displayThumb = new ItemEntity("" + DBUtil.DB_RECORD_DISPLAY_THUMB, Util.displayThumb ? Resource.TXT_YES : Resource.TXT_NO, Resource.SETTING_DISPLAY_THUMB);
        Vector vDisplayThumb = displayThumb.getItemChildrends("" + ItemEntity.TYPE_SETTING);
        String[] sDisplayThumb = new String[]{Resource.TXT_YES, Resource.TXT_NO};
        StringUtil.convertStringToVector(vDisplayThumb, sDisplayThumb);
        //Connection
        ItemEntity connection = new ItemEntity("" + DBUtil.DB_RECORD_CONNECTION, Util.connection, Resource.SETTING_CONNECTION);
        Vector vConnection = connection.getItemChildrends("" + ItemEntity.TYPE_SETTING);
        String[] sConnection = new String[]{DBUtil.CONNECTION_WIFI, DBUtil.CONNECTION_3G, DBUtil.CONNECTION_AUTO};
        StringUtil.convertStringToVector(vConnection, sConnection);

//        settings.addElement(themes);
        settings.addElement(displayCounter);
        settings.addElement(displayThumb);
        settings.addElement(connection);
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getCommand() == cmdView || evt.getSource() == listItem) {
            itemSelected(listItem.getSelectedIndex());
        }
    }

    protected void itemSelected(int index) {
        final ItemEntity selected = (ItemEntity) listItem.getSelectedItem();
        
        Vector items = selected.getItemChildrends("" + ItemEntity.TYPE_SETTING);
        final List list = new List(items);
        try {
            int selectedIndex = items.indexOf(selected.name);
            list.setSelectedIndex(selectedIndex);
        } catch (Exception e) {
        }
        DefaultListCellRenderer renderer = (DefaultListCellRenderer) list.getRenderer();
        renderer.setUIID("ListRenderer");
//        int selectedMarginLeft = renderer.getSelectedStyle().getMargin(Component.LEFT);
//        renderer.getUnselectedStyle().setMargin(Component.LEFT, selectedMarginLeft);
        renderer.setShowNumbers(false);
        renderer.setAlignment(Label.CENTER);

        final Dialog dialog = new Dialog();
        list.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                byte id = Byte.parseByte(selected.id);
                String value = list.getSelectedItem().toString();
                DBUtil.changeValue((id), value);
                selected.name = value;
                //set value into Util
                if (id == DBUtil.DB_RECORD_DISPLAY_COUNTER) {
                    int oldDisplayCounter = Util.displayCounter;
                    Util.displayCounter = Integer.parseInt(value);
                    if (oldDisplayCounter != Util.displayCounter) {
                        MainForm.mItemRoot.removeAllChilrends();
                        DBUtil.changeValue(id, value);
                    }
                } else if (id == DBUtil.DB_RECORD_DISPLAY_THUMB) {
                    Util.displayThumb = value.equals(Resource.TXT_NO) ? false : true;
//                    DBUtil.changeValue(id, value);
                } else if (id == DBUtil.DB_RECORD_CONNECTION) {
                    Util.connection = value;
//                    DBUtil.changeValue(DBUtil.DB_RECORD_DISPLAY_THUMB, value);
                } else if (id == DBUtil.DB_RECORD_THEMES) {
                    DialogUtil.showInfoDialog(Resource.TXT_WARRING_FUNTION_NOT_AVAIABLE);
                }
                dialog.dispose();
            }
        });
        int padding = Display.getInstance().getDisplayHeight() / 20;
        list.getUnselectedStyle().setMargin(padding, padding, 0, 0);
        list.getSelectedStyle().setMargin(padding, padding, 0, 0);
        //ANHNDT -- old
//        Command cmdBack = new Command(Resource.CMD_BACK);
//        Command[] cmds = new Command[]{cmdBack};
//        Container body = new Container(new BorderLayout());
//        body.setUIID("DialogContentPane");
//        body.addComponent(BorderLayout.CENTER, list);
//        Dialog.show(null, body, cmds,Dialog.TYPE_NONE,null);

        Command cmdBack = new Command(Resource.CMD_BACK) {

            public void actionPerformed(ActionEvent evt) {
                dialog.dispose();
            }
        };
        Container body = new Container(new BorderLayout());
        body.setUIID("DialogContentPane");
        body.setScrollable(false);
        body.addComponent(BorderLayout.CENTER, list);
        dialog.setScrollable(false);
        dialog.setLayout(new BorderLayout());
        dialog.addComponent(BorderLayout.CENTER, body);
        dialog.addCommand(cmdBack);
        dialog.setBackCommand(cmdBack);
        int pw = Display.getInstance().getDisplayWidth() * 5 / 100;
        int ph = (Display.getInstance().getDisplayHeight() - body.getPreferredH()) / 2 - padding - 10;

        dialog.show(ph, ph, pw, pw, false, false);
    }
}
